﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Wangzeran.CodeMaker.Core.Tag
{
    /// <summary>
    /// 标签处理
    /// </summary>
    public abstract class TagSupport : ITag
    {

        /// <summary>
        /// 匹配属性
        /// </summary>
        private const string PATTERN_PROPERTY = "\\S+=\".*?\"|\\S+='.*?'";

        /// <summary>
        /// 属性
        /// </summary>
        private IDictionary<string, string> m_Properties = new Dictionary<string, string>();
        /// <summary>
        /// 属性
        /// </summary>
        public IDictionary<string, string> Properties
        {
            get 
            {
                return m_Properties; 
            }
            set { m_Properties = value; }
        }

        #region ITag 成员
        /// <summary>
        /// 标签名称（全部小写）
        /// </summary>
        public abstract string TagName
        {
            get;
        }

        /// <summary>
        /// 标签处理
        /// </summary>
        /// <param name="tagStr">标签文本</param>
        /// <param name="value">标签值</param>
        /// <returns></returns>
        public virtual string Process(string tagStr, object value)
        {
            // 初始化属性
            this.InitProperties(tagStr);
            // 处理标签
            return this.ProcessTag(tagStr, value);
        }

        /// <summary>
        /// 标签处理
        /// </summary>
        /// <param name="tagStr">标签文本</param>
        /// <param name="value">标签值</param>
        /// <returns></returns>
        public abstract string ProcessTag(string tagStr, object value);

        /// <summary>
        /// 将此实例转换为System.String对象
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            Type t = this.GetType();
            StringBuilder sb = new StringBuilder();
            sb.Append(t.FullName);
            sb.Append(", ");
            sb.Append(t.Assembly.FullName);
            return sb.ToString();
        }

        #endregion

        /// <summary>
        /// 初始化属性
        /// </summary>
        /// <param name="tagStr">标签文本</param>
        protected void InitProperties(string tagStr)
        {
            m_Properties.Clear();
            // 匹配属性（xxx="xxx"）的字符串
            MatchCollection matchs = 
                Regex.Matches(tagStr, PATTERN_PROPERTY, RegexOptions.IgnoreCase);
            string[] property = null;
            foreach (Match match in matchs)
            {
                if (match.Success)
                {
                    property = GetTagProperty(match.Value);
                    m_Properties.Add(property[0].ToLower(), property[1]);
                }
            }
        }

        /// <summary>
        /// 获取属性名和属性值
        /// </summary>
        /// <param name="propertyStr">属性字符串（xxx="xxx"）</param>
        /// <returns></returns>
        private string[] GetTagProperty(string propertyStr)
        {
            // 处理属性字符串，取出属性名和属性值，并保存到数组中返回
            string[] property = propertyStr.Split('=');
            if (property.Length > 2)
            {
                for (int i = 2; i < property.Length; i++)
                {
                    property[1] += "=" + property[i];
                }
            }
            for (int i = 0; i < property.Length; i++)
            {
                property[i] = property[i].Replace("\"", String.Empty);
                property[i] = property[i].Replace("'", "\"");
            }
            return property;
        }

    }
}
